/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.framework
{
	
	public class MessageUtils
	{
		
		/**
		 * @param value
		 * @return
		 */
		public static function convertWhitespace( value:String ) : String
		{
			value = value.replace( /\t/g, "\\t" );
			value = value.replace( /\r/g, "\\r" );
			value = value.replace( /\n/g, "\\n" );
			value = value.replace( /\f/g, "\\f" );
			
			return value;
		}
		
		/**
		 * @param value
		 * @param char
		 * @return
		 */
		public static function trimCharacter( value:String, char:String ) : String
		{
			if (value == null) return "";
			
			var startIndex:int = 0;
			while (value.charAt(startIndex) == char)
			{
				startIndex++;
			}
			
			var endIndex:int = value.length - 1;
			while (value.charAt(endIndex) == char)
			{
				endIndex--;
			}
			
			if (endIndex >= startIndex)
			{
				return value.slice(startIndex, endIndex + 1);
			}
			else
			{
				return "";
			}
		}
		
		public static function clipString( value:String, start:int, maxLength:int ) : String
		{
			var clippedString:String = "";
			
			if (start > 0)
			{
				maxLength -= MessageString.EllipsisFormat.length;
				clippedString += MessageString.EllipsisFormat;
			}
			
			if (value.length - start > maxLength)
			{
				maxLength -= MessageString.EllipsisFormat.length;
				clippedString += value.substring(start, maxLength);
				clippedString += MessageString.EllipsisFormat;
			}
			else if (start > 0)
			{
				clippedString += value.substring(start);
			}
			else
			{
				clippedString += value;
			}
			
			return clippedString;
		}
		
		public static function calculateClipStart( expected:String, actual:String, mismatch:int, maxLength:int ) : int
		{
			// Case 1: Both strings fit on line
			var maxStringLength:int = Math.max(expected.length, actual.length);
			if (maxStringLength <= maxLength)
			{
				return -1;
			}
			
			// Case 2: Assume that the tail of each string fits on line
			var clipLength:int = maxLength - MessageString.EllipsisFormat.length;
			var tailLength:int = clipLength - mismatch;
			var clipStart:int = maxStringLength - clipLength;
			
			// Case 3: If it doesn't, center the mismatch position
			if ( clipStart > mismatch )
			{
				clipStart = Math.max( 0, mismatch - clipLength / 2 );
			}
			
			return clipStart;
		}
		
		public static function findMismatchPosition( expected:String, actual:String, start:int, ignoreCase:Boolean ) : int
		{
			var length:int = Math.min(expected.length, actual.length);
			
			expected =	(ignoreCase) ? expected.toLowerCase() : expected;
			actual =	(ignoreCase) ? actual.toLowerCase() : actual;
			
			var expectedArray:Array = expected.split("");
			var actualArray:Array = actual.split("");
			
			for ( var i:int=start; i<length; i++ )
			{
				if (expectedArray[i] != actualArray[i])
				{
					return i;
				}
			}
			
			// Strings have same content up to the length of the shorter string.
			// Mismatch occurs because string lengths are different, so show
			// that they start differing where the shortest string ends
			if (expected.length != actual.length)
			{
				return length;
			}
			
			return -1;
		}
		
	}
	
}